package servicesImp.upms;


import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import services.upms.AccountPermissionServices;
import services.upms.PermissionRoleServices;
import services.upms.PermissionServices;
import wt.dao.upms.PermissionMapper;
import wt.entity.upms.*;
import wt.staticData.TableName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/7.
 */
@Service
public class PermissionServicesImp extends IBaseServicesWithoutBLOBsImpl<Permission, PermissionExample> implements PermissionServices {

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    PermissionRoleServices permissionRoleServices;
    @Autowired
    AccountPermissionServices accountPermissionServices;


    public IBaseMapperWithoutBLOBs< Permission, PermissionExample> getMapper() {
        return permissionMapper;
    }

    @Override
    public String getTableName() {
        return  TableName.UPMS_PERMISSION;
    }



    /**
     * 查询角色具有的权限
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    public List<Permission> selectByRoleId(String roleId) throws Exception {

        PermissionRoleExample permissionRoleExample = new PermissionRoleExample();
        permissionRoleExample.createCriteria().andRoleIdEqualTo(roleId);
        List<PermissionRole> permissionRoleList = permissionRoleServices.selectByExample(permissionRoleExample);

        if (permissionRoleList.isEmpty()){
            return new ArrayList<Permission>();
        }


        List<String> permissionIdList = new ArrayList<String>();
        for (PermissionRole permissionRole : permissionRoleList) {
            permissionIdList.add(permissionRole.getPermissionId());
        }
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.setOrderByClause("orders");
        permissionExample.createCriteria().andIdIn(permissionIdList);
        return selectByExample(permissionExample);

    }

    /**
     * 查询账户具有的权限
     *
     * @param accountId
     * @return
     * @throws Exception
     */
    public List<Permission> selectByAccountId(String accountId) throws Exception {
        AccountPermissionExample accountPermissionExample = new AccountPermissionExample();
        accountPermissionExample.createCriteria().andAccountIdEqualTo(accountId);
        List<AccountPermission> accountPermissionList = accountPermissionServices.selectByExample(accountPermissionExample);
        List<String> permissionIdList = new ArrayList<String>();
        for (AccountPermission accountPermission : accountPermissionList) {
            permissionIdList.add(accountPermission.getPermissionId());
        }
        PermissionExample permissionExample = new PermissionExample();
        permissionExample.setOrderByClause("orders");
        permissionExample.createCriteria().andIdIn(permissionIdList);
        return selectByExample(permissionExample);
    }
}
